package cate.game.play.skill.passive.furniture;

import cate.common.table.d.GDCamp;
import cate.common.table.d.GDSkill;
import cate.game.attr.FightAttr;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.effect.SkillEffect;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 被阵营克制的效果减半；被攻击时降低攻击者40%恢复能力，攻击时降低最多2名目标30%恢复能力，不可叠加，
 *  目标血量低于50%时效率为150%，被击和攻击每回合只能触发其中1个
 */
public class 疫病抉择PH extends PassiveHandler {

	private double 克制系数;

	private int 被击buff;
	private int 攻击buff;
	private int 攻击buff人数;

	private int 检测血量;
	private int 被击强化buff;
	private int 攻击强化buff;

	//克制系数=-5000&被击buff=？？&攻击buff=？&攻击buff人数=2&
	//检测血量=5000&被击强化buff=？？&攻击强化buff=？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		克制系数 = args.getWDouble("克制系数", 0d);

		被击buff = args.getInt("被击buff", 0);
		攻击buff = args.getInt("攻击buff", 0);
		攻击buff人数 = args.getInt("攻击buff人数", 0);

		检测血量 = args.getInt("检测血量", 0);
		被击强化buff = args.getInt("被击强化buff", 0);
		攻击强化buff = args.getInt("攻击强化buff", 0);
	}

	@Override
	public void beforeSkillSettleAsTarget(SkillActionCtx action, SkillEffect effect) {
		if (!action.getSkill().cfg.isEffectType(GDSkill.EffectType.伤害)) {
			return;
		}
		if (!GDCamp.Type.counter(action.getActor().getCamp(), skill.owner.getCamp())) {
			return;
		}
		effect.getTargetCtx(skill.owner.getPid()).addActorAttr(new FightAttr(GDCamp.COUNTER_ATTR_STR).scale(克制系数));
	}

	@Override
	public void afterSkillSettleAsTarget(SkillActionCtx action, SkillEffect effect) {
		if (!action.getSkill().cfg.isEffectType(GDSkill.EffectType.伤害)) {
			return;
		}
		if (addBuff) {
			return;
		}
		addBuff(action, action.getActor(), false);
		addBuff = true;
	}

	@Override
	public void onSkillDone(SkillActionCtx action) {
		if (!action.getSkill().cfg.isEffectType(GDSkill.EffectType.伤害)) {
			return;
		}
		if (addBuff) {
			return;
		}
		action.getTargets().stream().limit(攻击buff人数).forEach(f -> addBuff(action, f, true));
		addBuff = true;
	}

	private boolean addBuff;

	private void addBuff(SkillActionCtx action, Fighter target, boolean attack) {
		target.buff.tryAddByTid(action, skill.owner, getBuff(attack, target));
	}

	private int getBuff(boolean attack, Fighter target) {
		boolean lowHp = target.attr.total().hpCurRate() <= 检测血量;
		if (lowHp) {
			if (attack) {
				return 攻击强化buff;
			} else {
				return 被击强化buff;
			}
		} else {
			if (attack) {
				return 攻击buff;
			} else {
				return 被击buff;
			}
		}
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		addBuff = false;
	}
}
